package com.xxx;

import java.util.Scanner;

public class BankMethod {
    private Bank bank;
    private Scanner sc = new Scanner(System.in);
    boolean exit = true;

    public BankMethod(Bank bank) {
        this.bank = bank;
    }

    public void star() {
        synchronized (BankMethod.class) {
            while (true) {
                System.out.println("-----------欢迎使用工商银行ATM机-----------");
                System.out.println("请输入您的银行卡:");
                String id = sc.next();
                if (bank.getId().equals(id)) {
                    System.out.println("请输入您的密码:");
                    String password = sc.next();
                    if (bank.getPassword().equals(password)) {
                        while (exit) {
                            System.out.println("请选择功能:1.取款 2.存款 3.查询余额 4.转账 5.退出");
                            int temp = sc.nextInt();
                            switch (temp) {
                                case 1:
                                    if (Thread.currentThread().getName().equals("小明")) {
                                        Withdrawal();
                                        break;
                                    } else
                                            exit=false;
                                case 2:
                                    if (Thread.currentThread().getName().equals("妈妈")&&bank.getMoney()<100) {
                                        deposit();
                                        break;
                                    } else{
                                        exit=false;
                                        System.out.println("您的钱还有呢~~");
                                        System.out.println("小明取钱");
                                    }break;
                                case 3:
                                    Show();
                                    break;
                            }
                        }
                        return;
                    } else {
                        System.out.println("你输入的密码有误!~~");
                    }
                } else {
                    System.out.println("您输入的id有误!~~");
                }
            }
        }

    }

    private void Show() {
        if (bank.getMoney()==0){
            System.out.println("当前没有钱");
        }
        System.out.println("您的钱还剩:"+bank.getMoney());
    }

    private void deposit() {
        bank.setMoney(bank.getMoney() + 2000);
        System.out.println("您的钱为:"+bank.getMoney());
    }

    private void Withdrawal() {
        if (bank.getMoney() <= 100) {
            System.out.println("钱不够了");
            System.out.println("妈妈存钱");
            exit=false;
            return;
        }
        bank.setMoney(bank.getMoney() - 100);
        System.out.println("您的钱为:"+bank.getMoney());
        }
    }
